import { good_fav, good_favlist, good_unfav, shopcart_add, shopcart_list, shopcart_sub, shopcart_clear } from '../../utils/api'
import { Toast } from 'antd-mobile'
export function create_colladd_action(payload) {
    return { type: 'ADD', payload }
}

export function create_sub_action(payload) {
    return { type: 'SUB', payload }
}

export function create_colllist_action(payload) {
    return { type: 'EDIT', payload }
}
// 取消收藏
export function create_unfav_action(payload) {
    return { type: 'DELETE', payload }
}

// 收藏一个店铺
export function create_collrequest_action(payload) {
    return function (dispatch) {
        // 在这里发请求
        let userid = JSON.parse(localStorage.getItem("userinfo")).id
        console.log('userid', userid)
        console.log('payload', payload.id)
        good_fav({ userid: userid, goodid: payload.id }).then((res) => {
            if (res.data.code == 200) {
                console.log(res.data)
                dispatch(create_colladd_action(payload))
                Toast.show({
                    icon: 'success',
                    content: '收藏成功！',
                })
            }

        })
    }
}
// 请求收藏列表
export function create_collListrequest_action(payload) {
    return function (dispatch) {
        // 在这里发请求
        let userid = JSON.parse(localStorage.getItem("userinfo")).id
        good_favlist({ userid: userid }).then((res) => {
            if (res.data.code == 200) {
                console.log('请求到的收藏列表', res.data.list)
                dispatch(create_colllist_action(res.data.list))
            }

        })
    }
}
// 取消收藏列表
export function create_unfavrequest_action(payload) {
    return function (dispatch) {
        // 在这里发请求
        let userid = JSON.parse(localStorage.getItem("userinfo")).id
        console.log('userid', userid)
        console.log('payload', payload.id)
        good_unfav({ userid: userid, goodid: payload.id }).then((res) => {
            if (res.data.code == 200) {
                console.log(res.data)
                dispatch(create_unfav_action(payload.id))
                Toast.show({
                    icon: 'fail',
                    content: '已取消收藏',
                })
            }

        })
    }
}
// ..........................................................

// 购物车相关
// 数量加1
export function create_shopadd_action(payload) {
    return { type: 'SHOPADD', payload }
}
// 数量减1
export function create_shopsub_action(payload) {
    return { type: 'SHOPSUB', payload }
}
// 清空
export function create_shopclear_action(payload) {
    return { type: 'SHOPCLEAR', payload }
}
// 获取商品列表
export function create_shopedit_action(payload) {
    return { type: 'SHOPEDIT', payload }
}

// 购物车数量加1
export function create_addshop_request_action(payload) {
    return function (dispatch) {
        // 在这里发请求
        let userid = JSON.parse(localStorage.getItem("userinfo")).id
        console.log('payload', payload.id)
        shopcart_add({ userid: userid, goodid: payload.id }).then((res) => {
            if (res.data.code == 200) {
                console.log(res.data)
                dispatch(create_shopadd_action(payload))
            }

        })
    }
}
// 购物车数量减1
export function create_subshop_request_action(payload) {
    return function (dispatch) {
        // 在这里发请求
        let userid = JSON.parse(localStorage.getItem("userinfo")).id
        console.log('payload', payload.id)
        shopcart_sub({ userid: userid, goodid: payload.id }).then((res) => {
            if (res.data.code == 200) {
                console.log(res.data)
                dispatch(create_shopsub_action(payload))

            }

        })
    }
}
// 获取购物车列表
export function create_shopedit_request_action(payload) {
    return function (dispatch) {
        // 在这里发请求
        let userid = JSON.parse(localStorage.getItem("userinfo")).id
        shopcart_list({ userid: userid }).then((res) => {
            if (res.data.code == 200) {
                console.log('请求到的购物车列表', res.data.list)
                dispatch(create_shopedit_action(res.data.list))
            }

        })
    }
}
//清空购物车
export function create_shopclear_request_action(payload) {
    return function (dispatch) {
        // 在这里发请求
        let userid = JSON.parse(localStorage.getItem("userinfo")).id
        shopcart_clear({ userid: userid }).then((res) => {
            if (res.data.code == 200) {
                console.log(res.data)
                dispatch(create_shopclear_action())
            }

        })
    }
}